package com.gjun.struts.action;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.GjunsystemUser;
import com.gjun.struts.resource.GjunSystemUserResourceIf;
import com.gjun.struts.util.MD5;

public class UpdateSystemUserAction {
    private String result;
    
    private String new_passwd;
    
    private String now_passwd;
    
    private String email;
    
    private String qq;
    
    private String tel;
    
    private GjunSystemUserResourceIf userResourceDAO;

    
    
	public String getNow_passwd() {
		return now_passwd;
	}

	public void setNow_passwd(String now_passwd) {
		this.now_passwd = now_passwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNew_passwd() {
		return new_passwd;
	}

	public void setNew_passwd(String new_passwd) {
		this.new_passwd = new_passwd;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public GjunSystemUserResourceIf getUserResourceDAO() {
		return userResourceDAO;
	}

	public void setUserResourceDAO(GjunSystemUserResourceIf userResourceDAO) {
		this.userResourceDAO = userResourceDAO;
	}
    
    public String execute(){
    	
    	// 先判断密码
    	String accountPasswd = ((GjunsystemUser)ServletActionContext.getRequest().getSession().getAttribute("loginAccount")).getPasswd();
    	
    	if(!MD5.toMD5(now_passwd).equals(accountPasswd)){   // admin == accountPasswd   dasdqweqweasdasdasdazasd
    		result = "passwdCheckFaild";
    		return "success";
    	}
    	
    	int id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("accountId");
    	boolean isHaveEmail = userResourceDAO.loadOtherUserExistEmail(email,id);
    	if(isHaveEmail){
			result = "haveEmail";
			return "success";
		}
    	
    	try {
			userResourceDAO.updateSystemUserInfo(id, MD5.toMD5(new_passwd), tel, qq, email);
			ServletActionContext.getRequest().getSession().invalidate();
			result = "updateSystemUserSuccess";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "updateSystemUserFaild";
		}
    	return "success";
    }
}
